<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Service\Attraction_spot;

use Plugin\Attractions\AttractionManagement\Repository\Attraction_spot\AttractionSpotRepository;
use Plugin\Attractions\AttractionManagement\Repository\Attraction_spot_position\AttractionSpotPositionRepository;
use Plugin\Attractions\AttractionManagement\Repository\Attraction_type\AttractionTypeRepository;
use Plugin\Attractions\AttractionManagement\Repository\AttractionRepository;
use App\Service\IService;

/**
 * 点位表服务类
 */
final class AttractionSpotService extends IService
{
    public function __construct(
        protected readonly AttractionSpotRepository $repository,
        protected readonly AttractionSpotPositionRepository $positionRepository,
        protected readonly AttractionTypeRepository $typeRepository,
        protected readonly AttractionRepository $attractionRepository
    ) {}

    /**
     * 获取分页列表，包含关联信息
     */
    public function page(array $params = [], int $page = 1, int $pageSize = 10): array
    {
        // 使用父类的page方法
        $result = parent::page($params, $page, $pageSize);
        
        // 为每条记录添加关联信息
        if (!empty($result['list'])) {
            foreach ($result['list'] as &$item) {
                // 获取位置名称
                if (!empty($item['position_id'])) {
                    try {
                        $position = $this->positionRepository->findById($item['position_id']);
                        $item['position_name'] = $position['name'] ?? '未知位置';
                    } catch (\Exception $e) {
                        $item['position_name'] = '未知位置';
                    }
                }
                
                // 获取类型名称
                if (!empty($item['type_id'])) {
                    try {
                        $type = $this->typeRepository->findById($item['type_id']);
                        $item['type_name'] = $type['name'] ?? '未知类型';
                    } catch (\Exception $e) {
                        $item['type_name'] = '未知类型';
                    }
                }
                
                // 获取景区名称
                if (!empty($item['attraction_id'])) {
                    try {
                        $attraction = $this->attractionRepository->findById($item['attraction_id']);
                        $item['attraction_name'] = $attraction['name'] ?? '未知景区';
                    } catch (\Exception $e) {
                        $item['attraction_name'] = '未知景区';
                    }
                }
            }
        }
        
        return $result;
    }
}